package com.example.compose

import android.content.res.Resources
import android.graphics.BitmapShader
import android.graphics.Paint
import android.graphics.Shader
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateIntAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.ShaderBrush
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.compose.biometricauthentic.BiometricScreen
import com.example.compose.jetsurvey.JetsurveyNavHost
import com.example.compose.ui.theme.ComposeTheme
import dagger.hilt.android.AndroidEntryPoint
import java.lang.Math.cos
import java.lang.Math.sin

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
//        installSplashScreen()
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        setContent {
            ComposeTheme {
//                JetsurveyNavHost()
//                LatterByLatterAnimate()
                BiometricScreen()
            }
        }
    }
}
// 逐个字符地制作文本动画
@Composable
fun LatterByLatterAnimate(modifier: Modifier = Modifier) {
    Box(modifier = modifier
        .fillMaxSize()
        .statusBarsPadding()) {
        val fullText =
            "This should be animated letter by letter and \\uD83D\\uDE00 while appearing.\\n\\n\" +\n" +
                    "                \"これは、表示中に文字ごとにアニメーション化され、 \\uD83D\\uDE00 になるはずです。\\n\\n\" +\n" +
                    "                \"Kore wa, hyōji-chū ni moji-goto ni animēshon-ka sa re, \\uD83D\\uDE00 ni naru hazudesu.\\n\\n"
        var isTextVisible by rememberSaveable {
            mutableStateOf(false)
        }
        val currentCharIndex by animateIntAsState(
            targetValue = if (isTextVisible) fullText.length else 0,
            animationSpec = tween(durationMillis = 9000, easing = LinearEasing),
            label = ""
        )
        LaunchedEffect(key1 = fullText) {
            isTextVisible = true
        }

        Text(text = fullText.subSequence(0, currentCharIndex).toString())
    }
}

@Preview
@Composable
fun BackgroundBannerPreview() {
    BackgroundBanner(avatarRes = R.drawable.bella)
}

@Composable
fun BackgroundBanner(@DrawableRes avatarRes: Int, res: Resources = LocalContext.current.resources) {
    val brush = remember(key1 = avatarRes) {
        ShaderBrush(
            BitmapShader(
                ImageBitmap.imageResource(res = res, id = avatarRes).asAndroidBitmap(),
                Shader.TileMode.DECAL,
                Shader.TileMode.CLAMP
            )
        )
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(brush = brush)
    ) {

    }
}

@Composable
fun RotatingRings() {
    // 创建两个旋转动画，分别用于内外环
    val innerRotationAnimation = rememberInfiniteTransition(label = "")
    val innerRotationAngle by innerRotationAnimation.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 2000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ), label = ""
    )

    val outerRotationAnimation = rememberInfiniteTransition(label = "")
    val outerRotationAngle by outerRotationAnimation.animateFloat(
        initialValue = 0f,
        targetValue = -360f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 3000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ), label = ""
    )

    Canvas(modifier = Modifier.fillMaxSize()) {
        val centerX = size.width / 2
        val centerY = size.height / 2
        val innerRadius = 100f.dp.toPx()
        val outerRadius = 150f.dp.toPx()

        // 绘制内环
        drawRotatedRing(
            centerX,
            centerY,
            innerRadius,
            16f.dp.toPx(),
            Color.Red,
            innerRotationAngle,
            this
        )

        // 绘制外环
        drawRotatedRing(
            centerX,
            centerY,
            outerRadius,
            16f.dp.toPx(),
            Color.Blue,
            outerRotationAngle,
            this
        )
    }
}

fun drawRotatedRing(
    centerX: Float,
    centerY: Float,
    radius: Float,
    strokeWidth: Float,
    color: Color,
    rotation: Float,
    drawScope: DrawScope
) {
    drawScope.rotate(rotation, pivot = Offset(centerX, centerY)) {
        drawArc(
            color = color,
            startAngle = 0f,
            sweepAngle = 350f,
            useCenter = false,
            topLeft = Offset(centerX - radius, centerY - radius),
            size = Size(radius * 2, radius * 2),
            style = Stroke(width = strokeWidth)
        )
    }
}

fun drawRing(center: Offset, radius: Float, rotation: Float, color: Color, drawScope: DrawScope) {
    drawScope.rotate(rotation, pivot = center) {
        drawCircle(color = color, radius = radius)
    }
}


@Composable
fun JianWangSanGuayuQiankun() {
    val heavens = listOf("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛")
    val earths = listOf("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")

    val transition = rememberInfiniteTransition(label = "")

    val rotation by transition.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 5000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ), ""
    )

    Canvas(modifier = Modifier.size(200.dp)) {
        val centerX = size.width / 2
        val centerY = size.height / 2
        val radius = size.minDimension / 3

        // 绘制圆圈
        drawCircle(
            color = Color.Transparent,
            radius = radius,
            style = Stroke(width = 3.dp.toPx())
        )

        // 绘制卦象
        val gapAngle = 360 / 8
        val halfGapAngle = gapAngle / 2
        repeat(8) { index ->
            val angle = Math.toRadians((90 - index * gapAngle).toDouble())
            val startX = centerX + (radius * cos(angle)).toFloat()
            val startY = centerY - (radius * sin(angle)).toFloat()
            val endX =
                centerX + (radius * cos(angle + Math.toRadians(gapAngle.toDouble()))).toFloat()
            val endY =
                centerY - (radius * sin(angle + Math.toRadians(gapAngle.toDouble()))).toFloat()
            drawLine(
                color = Color.Black,
                start = Offset(startX, startY),
                end = Offset(endX, endY),
                strokeWidth = 3.dp.toPx()
            )

            // 绘制天干
            val heavensTextAngle = Math.toRadians((90 - index * gapAngle + halfGapAngle).toDouble())
            val heavensTextX =
                centerX + (radius * 0.9 * kotlin.math.cos(heavensTextAngle)).toFloat()
            val heavensTextY = centerY - (radius * 0.9 * sin(heavensTextAngle)).toFloat()
            drawIntoCanvas {
                it.nativeCanvas.drawText(
                    heavens[index],
                    heavensTextX,
                    heavensTextY,
                    Paint().apply {
                        color = Color.Black.toArgb()
                        textSize = 20f
                        textAlign = Paint.Align.CENTER
                    }
                )
            }
        }

        // 绘制地支
        val earthsGapAngle = 360 / 12
        val halfEarthsGapAngle = earthsGapAngle / 2
        repeat(earths.size) { index ->
            val angle = Math.toRadians((90 - index * earthsGapAngle).toDouble())
            val startX = centerX + (radius * cos(angle)).toFloat()
            val startY = centerY - (radius * sin(angle)).toFloat()
            val endX =
                centerX + (radius * cos(angle + Math.toRadians(earthsGapAngle.toDouble()))).toFloat()
            val endY =
                centerY - (radius * sin(angle + Math.toRadians(earthsGapAngle.toDouble()))).toFloat()
            drawLine(
                color = Color.Black,
                start = Offset(startX, startY),
                end = Offset(endX, endY),
                strokeWidth = 3.dp.toPx()
            )

            // 绘制地支
            val earthsTextAngle =
                Math.toRadians((90 - index * earthsGapAngle + halfEarthsGapAngle).toDouble())
            val earthsTextX = centerX + (radius * 0.7 * cos(earthsTextAngle)).toFloat()
            val earthsTextY = centerY - (radius * 0.7 * sin(earthsTextAngle)).toFloat()
            drawIntoCanvas {
                it.nativeCanvas.drawText(
                    earths[index],
                    earthsTextX,
                    earthsTextY,
                    Paint().apply {
                        color = Color.Black.toArgb()
                        textSize = 16f
                        textAlign = Paint.Align.CENTER
                    }
                )
            }
        }

        // 绘制动态特效
        val effectRadius = radius * 0.8f
        val effectStartX =
            centerX + (effectRadius * cos(Math.toRadians(rotation.toDouble()))).toFloat()
        val effectStartY =
            centerY - (effectRadius * sin(Math.toRadians(rotation.toDouble()))).toFloat()
        val effectEndX =
            centerX + (effectRadius * cos(Math.toRadians(rotation.toDouble() + 180))).toFloat()
        val effectEndY =
            centerY - (effectRadius * sin(Math.toRadians(rotation.toDouble() + 180))).toFloat()
        drawLine(
            color = Color.Red,
            start = Offset(effectStartX, effectStartY),
            end = Offset(effectEndX, effectEndY),
            strokeWidth = 3.dp.toPx()
        )
    }
}

@Composable
fun JianWangSanGuayu() {
    val transition = rememberInfiniteTransition(label = "")

    val rotation by transition.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 5000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ), label = ""
    )

    Canvas(modifier = Modifier.size(200.dp)) {
        val centerX = size.width / 2
        val centerY = size.height / 2
        val radius = size.minDimension / 3

        // 绘制圆圈
        drawCircle(
            color = Color.Transparent,
            radius = radius,
            style = Stroke(width = 3.dp.toPx())
        )

        // 绘制卦象
        val gapAngle = 360 / 8
        val halfGapAngle = gapAngle / 2
        repeat(8) { index ->
            val angle = Math.toRadians((90 - index * gapAngle).toDouble())
            val startX = centerX + (radius * cos(angle)).toFloat()
            val startY = centerY - (radius * sin(angle)).toFloat()
            val endX =
                centerX + (radius * cos(angle + Math.toRadians(gapAngle.toDouble()))).toFloat()
            val endY =
                centerY - (radius * sin(angle + Math.toRadians(gapAngle.toDouble()))).toFloat()
            drawLine(
                color = Color.Black,
                start = Offset(startX, startY),
                end = Offset(endX, endY),
                strokeWidth = 3.dp.toPx()
            )
        }

        // 绘制动态特效
        val effectRadius = radius * 0.8f
        val effectStartX =
            centerX + (effectRadius * cos(Math.toRadians(rotation.toDouble()))).toFloat()
        val effectStartY =
            centerY - (effectRadius * sin(Math.toRadians(rotation.toDouble()))).toFloat()
        val effectEndX =
            centerX + (effectRadius * cos(Math.toRadians(rotation.toDouble() + 180))).toFloat()
        val effectEndY =
            centerY - (effectRadius * sin(Math.toRadians(rotation.toDouble() + 180))).toFloat()
        drawLine(
            color = Color.Red,
            start = Offset(effectStartX, effectStartY),
            end = Offset(effectEndX, effectEndY),
            strokeWidth = 3.dp.toPx()
        )
    }
}

@Composable
fun ChineseCompass() {
    val directions = listOf("南", "东南", "东", "东北", "北", "西北", "西", "西南")
    val annotations = listOf("财", "官", "福", "医", "合", "杀", "宅", "绝")

    Box(
        modifier = Modifier
            .size(240.dp)
            .padding(16.dp)
            .background(Color.Gray)
    ) {
        Canvas(modifier = Modifier.fillMaxSize()) {
            val radius = size.width / 2
            val centerX = size.width / 2
            val centerY = size.height / 2

            val gapAngle = 360 / directions.size
            val halfGapAngle = gapAngle / 2

            repeat(directions.size) { index ->
                val startAngle = index * gapAngle.toFloat()
                val sweepAngle = gapAngle.toFloat()

                drawArc(
                    color = Color.White,
                    startAngle = startAngle - halfGapAngle,
                    sweepAngle = sweepAngle,
                    useCenter = true,
                    topLeft = Offset(centerX - radius, centerY - radius),
                    size = Size(radius * 2, radius * 2)
                )

                /* // 绘制方位标签
                 val labelAngle = Math.toRadians(startAngle + halfGapAngle.toDouble())
                 val labelX = centerX + (radius * 0.7 * kotlin.math.cos(labelAngle)).toFloat()
                 val labelY = centerY + (radius * 0.7 * kotlin.math.sin(labelAngle)).toFloat()

                 drawIntoCanvas {
                     it.nativeCanvas.drawText(
                         directions[index],
                         labelX,
                         labelY,
                         Paint().apply {
                             color = Color.Black.toArgb()
                             textSize = 18f
                         }
                     )
                 }

                 // 绘制注释
                 val annotationAngle = Math.toRadians(startAngle.toDouble())
                 val annotationX = centerX + (radius * 0.85 * kotlin.math.cos(annotationAngle)).toFloat()
                 val annotationY = centerY + (radius * 0.85 * kotlin.math.sin(annotationAngle)).toFloat()

                 drawIntoCanvas {
                     it.nativeCanvas.drawText(
                         annotations[index],
                         annotationX,
                         annotationY,
                         Paint().apply {
                             color = Color.Black.toArgb()
                             textSize = 16f
                         }
                     )
                 }*/
            }

            /*// 绘制中心圆
            drawCircle(
                color = Color.White,
                center = Offset(centerX, centerY),
                radius = radius * 0.2f
            )

            // 绘制指针
            drawLine(
                color = Color.Black,
                start = Offset(centerX, centerY - radius * 0.2f),
                end = Offset(centerX, centerY + radius * 0.4f),
                strokeWidth = 4f
            )*/
        }
    }
}

@Composable
fun PreviewChineseCompass() {
    ChineseCompass()
}